package com.neo.pub.service.impl;

import com.neo.common.annotation.DataSource;
import com.neo.common.enums.DataSourceType;
import com.neo.common.utils.StringUtils;
import com.neo.pub.domain.Patient;
import com.neo.pub.domain.QFPatient;
import com.neo.pub.domain.SMSUser;
import com.neo.pub.mapper.PatientMapper;
import com.neo.pub.mapper.QFPatientMapper;
import com.neo.pub.mapper.SMSMapper;
import com.neo.pub.service.IPatientService;
import com.neo.pub.service.IQFPatientService;
import com.neo.pub.utils.SMSUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 用户 业务层处理
 *
 * @author tr
 */
@Service
public class QFPatientServiceImpl implements IQFPatientService {
    private static final Logger log = LoggerFactory.getLogger(QFPatientServiceImpl.class);

    @Autowired
    private QFPatientMapper qfPatientMapper;

    @Autowired
    private SMSMapper smsMapper;

    @Autowired
    private SMSUtils smsUtils;

    @Override
    public void sendQFMsg() {
        this.sendCurrentDeptMsg();
        this.sendLastDeptMsg();
    }

    private void sendCurrentDeptMsg() {
        //发送给收费处
//        StringBuilder summary = new StringBuilder();

        //入院科室分组
        List<String> currentDeptList = qfPatientMapper.selectDeptCodeByGroup();
        if (CollectionUtils.isNotEmpty(currentDeptList)) {
            //先处理入院科室短信
            for (String deptCode : currentDeptList) {
                //group 身份证号
                List<String> ids = qfPatientMapper.selectPatientGroupByIdNumber(deptCode);
                for (String id : ids) {
                    //本次入院
                    List<QFPatient> qfPatients = qfPatientMapper.selectPatientByDeptCodeAndIdNumber(deptCode, id);
                    StringBuilder sb = new StringBuilder();
                    //拼凑短信内容
                    for (int j = 0; j < qfPatients.size(); j++) {
                        QFPatient patient = qfPatients.get(j);
                        if (j == 0) {
                            sb.append("患者 ")
                                    .append(patient.getName())
                                    .append("(")
                                    .append(patient.getPatientId())
                                    .append(",")
                                    .append(patient.getDeptName())
                                    .append(")本次在你科住院,存在出院未结算情况,共")
                                    .append(qfPatients.size())
                                    .append("次,未结算住院号:");
                        }
                        sb.append(patient.getLastInpatientNo())
                                .append("(出院科室:")
                                .append(patient.getLastDeptName())
                                .append(",出院时间:")
                                .append(patient.getLastDischargedDate())
                                .append(")")
                                .append(";");
                        if(j == qfPatients.size()-1){
                            sb.append("(仅供参考)");
                        }
                    }
                    this.smsSend(deptCode, sb);
                    //给收费处崔森发送一遍
                    this.smsSend4Charge(sb);
//                    summary.append(sb);
                }
            }
        }
        //汇总后发给收费处崔森
//        List<String> feePhoneList = smsMapper.selectPhoneListByLevel("chiefcharge");
//        if (CollectionUtils.isNotEmpty(feePhoneList)) {
//            smsUtils.sendMsg(feePhoneList, summary.toString());
//        }
    }

    private void smsSend4Charge(StringBuilder sb) {
        List<String> chargePhoneList = smsMapper.selectPhoneListByLevel("chiefcharge");
        if (CollectionUtils.isNotEmpty(chargePhoneList)) {
            smsUtils.sendMsg(chargePhoneList, sb.toString());
        }
    }

    private void sendLastDeptMsg() {
        //再处理前次出院科室短信
        //科室分组
        List<String> lastDeptList = qfPatientMapper.selectLastDeptCodeByGroup();
        if (CollectionUtils.isNotEmpty(lastDeptList)) {
            for (String lastDeptCode : lastDeptList) {
                //group 身份证号
                List<String> ids = qfPatientMapper.selectLastPatientGroupByIdNumber(lastDeptCode);
                for (String id : ids) {
                    //前次入院
                    List<QFPatient> qfPatients = qfPatientMapper.selectLastPatientByDeptCodeAndIdNumber(lastDeptCode, id);
                    StringBuilder sb = new StringBuilder();
                    //拼凑短信内容
                    for (int j = 0; j < qfPatients.size(); j++) {
                        QFPatient patient = qfPatients.get(j);
                        if (j == 0) {
                            sb.append("你科出院患者");
                        }
                        sb.append(patient.getLastInpatientNo())
                                .append("(出院科室:")
                                .append(patient.getLastDeptName())
                                .append(",出院时间:")
                                .append(patient.getLastDischargedDate())
                                .append("),");
                        if(j == qfPatients.size()-1){
                            sb.append("存在出院未结算情况，现在再次入院，信息为:")
                                    .append(patient.getName())
                                    .append("(住院号:")
                                    .append(patient.getPatientId())
                                    .append(",住院科室:")
                                    .append(patient.getDeptName())
                                    .append(")。(仅供参考)");
                        }
                    }
                    this.smsSend(lastDeptCode, sb);
//                    summary.append(sb);
                }
            }
        }
    }

    private void smsSend(String wardCode, StringBuilder sb) {
        List<String> phoneList = smsMapper.selectPhoneListByWardCode(wardCode);
        if (CollectionUtils.isNotEmpty(phoneList)) {
            smsUtils.sendMsg(phoneList, sb.toString());
        }
    }

}
